﻿/*
 * Created by SharpDevelop.
 * User: rogerio
 * Date: 12/10/2009
 * Time: 10:09 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using csharp.rad.general;

namespace csharp.rad.pos
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : KryptonForm
	{
		csharp.rad.security.WinApp MyApp = new csharp.rad.security.WinApp();
		Configuration config = new Configuration();		
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		private void showForm(KryptonForm form, bool modal)
		{
			try
			{
				if (modal)
					form.ShowDialog();
				else
					form.Show();
			}
			finally
			{
				form.Dispose();
			}			
		}	
		
		void BtnQuickSaleClick(object sender, EventArgs e)
		{
			frmQuickSale form = new frmQuickSale();
			
			showForm(form, true);
		}
		
		void MainFormLoad(object sender, EventArgs e)
		{
			//Valida sistema
			general.Company company = new general.Company();
			
			if (!MyApp.Initialize(System.Reflection.Assembly.GetExecutingAssembly()))
			{
				KryptonMessageBox.Show("Banco de dados não conectado!" + csharp.common.Database.result.ErrorMessage);
				this.Close();
				return;
			}
			
			if (!System.IO.File.Exists(config.path))
				config.doSerialization();
			
			config.deserialize();
			
			if (config.showFormChooseCompany)
			{
				frmChooseCompany form = new frmChooseCompany();
				
				showForm(form, true);
			}
			
			company.id = config.defaultCompanyId;
			
			if (!company.get())
			{
				KryptonMessageBox.Show("Empresa padrão não cadastrada!\nContacte o suporte!");
				System.Environment.Exit(System.Environment.ExitCode);
			}
			csharp.Information.companyId = company.id;
			csharp.Information.companyName = company.person.name;
			
			csharp.Information.userId = MyApp.ApplicationUser.Id;
			csharp.Information.userName = MyApp.ApplicationUser.Name;
			
			//lblUserName.Text = MyApp.ApplicationUser.Name;
			//lblCompanyName.Text = csharp.Information.companyName;			
		}
	}
}
